//
//  WeiboCellViewModel.swift
//  weiboSwift1.0
//
//  Created by xuNick on 16/4/20.
//  Copyright © 2016年 NN. All rights reserved.
//

import UIKit

class HomeVCViewModel: NSObject {
    
    var weiboLists = [WeiboCellViewModel]()
    
    
   
    
    
    
    
    
    //请求数据
    func loadWeiboList(isAnimating: Bool,upDown:Bool, finished:(weiboList:[WeiboCellViewModel]) -> ()){
        
        if isAnimating == true {
            return
        }
        
        var max_id :Int64 = 0
        
        var since_id: Int64 = 0
        
        max_id = (weiboLists.last?.status?.id) ?? 0
        
        since_id = (weiboLists.first?.status?.id) ?? 0
        print(since_id)
        print(max_id)
       
        if max_id != 0{
            max_id--
        }
        
        if !upDown{
            since_id = 0
        }else{
            max_id = 0
        }
        

        
        let urlString:String = "https://api.weibo.com/2/statuses/home_timeline.json"
        let params = [
            "access_token": UserAccountViewModel.shareUserAccountViewModel.account?.access_token ?? "",
            "since_id": "\(since_id)",
            "max_id": "\(max_id)"
        ]
      
        NetworkingTool.shareWorkTools.request(urlString: urlString, parameters: params) { (reponseObject, error) -> () in
            if error != nil {
                print(error)
                return
            }
            
            guard let dicts = (reponseObject as! [String: AnyObject])["statuses"] as? [[String: AnyObject]] else{
                return
            }
            //空数组
            var tempArray = [WeiboCellViewModel]()
            
            
            
            for value in dicts{
                let status = WeiboStatus(dict: value)
                let cellViewModel = WeiboCellViewModel()
                cellViewModel.status = status
                
                tempArray.append(cellViewModel)
            }
            
            
//            self.weiboLists += tempArray
            
            //如果是单图就下载下来
            
            let group = dispatch_group_create()
            
            for value in tempArray{
                if value.pic_me?.count == 1{
                    dispatch_group_enter(group)
                    value.pic_me?.first?.sd_setImageWithURL(NSURL(string: (value.status?.pic_urls?.first!.thumbnail_pic)!) , completed: { (image, _, _, _) -> Void in
                        
                        value.pic_me?.first?.bounds = CGRect(origin: CGPointZero, size:  CGSize(width: (value.pic_me?.first?.bounds.width)! * Kscale, height: (value.pic_me?.first?.bounds.height)! * Kscale))
                        
                        
                        if value.pic_me?.first?.image?.size.height > 100{
                            value.pic_me?.first?.flagLong.hidden = false
                        }else{
                            value.pic_me?.first?.flagLong.hidden = true
                        }
                        
                        dispatch_group_leave(group)
                    })
                    
                }else if value.pic_retweet?.count == 1{
                    
                    dispatch_group_enter(group)
                    value.pic_retweet?.first?.sd_setImageWithURL(NSURL(string: (value.status?.retweeted_status?.pic_urls!.first!.thumbnail_pic)!) , completed: { (image, _, _, _) -> Void in
//                        image.size = CGSize(width: image.size.width * Kscale, height: image.size.height * Kscale)
                        value.pic_retweet?.first?.bounds = CGRect(origin: CGPointZero, size:  CGSize(width: (value.pic_retweet?.first?.bounds.width)! * Kscale, height: (value.pic_retweet?.first?.bounds.height)! * Kscale))
                        if value.pic_retweet?.first?.image?.size.height > 100{
                            value.pic_retweet?.first?.flagLong.hidden = false
                        }else{
                            value.pic_retweet?.first?.flagLong.hidden = true
                        }

                        
//                        value.pic_retweet?.first?.transform = CGAffineTransformMakeScale(Kscale, Kscale)
                        dispatch_group_leave(group)
                    })
                    
                    
                }
                
                
                
            }
            
            dispatch_group_notify(group, dispatch_get_main_queue()) { () -> Void in
                
                print("新微博\(tempArray.count)条")
                
                if upDown{
                    self.weiboLists = tempArray + self.weiboLists
                }else{
                     self.weiboLists = self.weiboLists +  tempArray
                }
             
                
                finished(weiboList: self.weiboLists)
                // - 回调界面刷新
               
            }

            
            
            
            
            
            
        }
    }
    
}
